home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu248.dms / pu248.adf / Intuition / Menus / Example2.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  17KB  |  427 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Menus                       Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program opens a normal window to which we connect a menu strip. */
  21. /* The menu will look like this:                                        */
  22. /*                                                                      */
  23. /* Edit                                                                 */
  24. /* ----------                                                           */
  25. /* | Style -----------------                                            */
  26. /* --------| v Plain       |                                            */
  27. /*         |   Bold        |                                            */
  28. /*         |   Underlined  |                                            */
  29. /*         |   Italic      |                                            */
  30. /*         -----------------                                            */
  31. /*                                                                      */
  32. /* This example is very similar to Example1, but we have this time put  */
  33. /* the edit styles in a subitem box which is connected to the one and   */
  34. /* only item box called "Style"                                         */
  35.  
  36.  
  37.  
  38. #include <intuition/intuition.h>
  39.  
  40.  
  41.  
  42. struct IntuitionBase *IntuitionBase;
  43.  
  44.  
  45.  
  46. /*************************************************************************/
  47. /*                      F O U R T H   S U B I T E M                      */
  48. /*************************************************************************/
  49.  
  50. /* The text for the fourth subitem: */
  51. struct IntuiText my_fourth_text=
  52. {
  53.   2,            /* FrontPen, black. */
  54.   0,            /* BackPen, not used since JAM1. */
  55.   JAM1,         /* DrawMode, do not change the background. */
  56.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  57.                 /* This will leave enough space for the check mark. */
  58.   1,            /* TopEdge, 1 line down. */
  59.   NULL,         /* TextAttr, default font. */
  60.   "Italic",     /* IText, the string. */
  61.   NULL          /* NextItem, no link to other IntuiText structures. */
  62. };
  63.  
  64. /* The MenuItem structure for the fourth subitem: */
  65. struct MenuItem my_fourth_subitem=
  66. {
  67.   NULL,            /* NextItem, this is the last subitem in the list. */
  68.   50,              /* LeftEdge, 50 pixels out. */
  69.   35,              /* TopEdge, 35 lines down. */
  70.   150,             /* Width, 150 pixels wide. */
  71.   10,              /* Height, 10 lines high. */
  72.   ITEMTEXT|        /* Flags, render this item with text. */
  73.   ITEMENABLED|     /*        this item will be enabled. */
  74.   CHECKIT|         /*        it is an attribute item. */
  75.   HIGHCOMP,        /*        complement the colours when highlihted. */
  76.   0x00000001,      /* MutualExclude, mutualexclude the first subitem. */
  77.   (APTR) &my_fourth_text, /* ItemFill, pointer to the text. */
  78.   NULL,            /* SelectFill, nothing since we complement the col. */
  79.   0,               /* Command, no command-key sequence. */
  80.   NULL,            /* SubItem, ignored by Intuition. */
  81.   MENUNULL,        /* NextSelect, no items selected. */
  82. };
  83.  
  84.  
  85.  
  86. /*************************************************************************/
  87. /*                       T H I R D   S U B I T E M                       */
  88. /*************************************************************************/
  89.  
  90. /* The text for the third subitem: */
  91. struct IntuiText my_third_text=
  92. {
  93.   2,            /* FrontPen, black. */
  94.   0,            /* BackPen, not used since JAM1. */
  95.   JAM1,         /* DrawMode, do not change the background. */
  96.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  97.                 /* This will leave enough space for the check mark. */
  98.   1,            /* TopEdge, 1 line down. */
  99.   NULL,         /* TextAttr, default font. */
  100.   "Underlined", /* IText, the string. */
  101.   NULL          /* NextItem, no link to other IntuiText structures. */
  102. };
  103.  
  104. /* The MenuItem structure for the third subitem: */
  105. struct MenuItem my_third_subitem=
  106. {
  107.   &my_fourth_subitem, /* NextItem, linked to the fourth subitem. */
  108.   50,              /* LeftEdge, 50 pixels out. */
  109.   25,              /* TopEdge, 25 lines down. */
  110.   150,             /* Width, 150 pixels wide. */
  111.   10,              /* Height, 10 lines high. */
  112.   ITEMTEXT|        /* Flags, render this item with text. */
  113.   ITEMENABLED|     /*        this item will be enabled. */
  114.   CHECKIT|         /*        it is an attribute item. */
  115.   HIGHCOMP,        /*        complement the colours when highlihted. */
  116.   0x00000001,      /* MutualExclude, mutualexclude the first subitem. */
  117.   (APTR) &my_third_text, /* ItemFill, pointer to the text. */
  118.   NULL,            /* SelectFill, nothing since we complement the col. */
  119.   0,               /* Command, no command-key sequence. */
  120.   NULL,            /* SubItem, ignored by Intuition. */
  121.   MENUNULL,        /* NextSelect, no items selected. */
  122. };
  123.  
  124.  
  125.  
  126. /*************************************************************************/
  127. /*                      S E C O N D   S U B I T E M                      */
  128. /*************************************************************************/
  129.  
  130. /* The text for the second subitem: */
  131. struct IntuiText my_second_text=
  132. {
  133.   2,          /* FrontPen, black. */
  134.   0,          /* BackPen, not used since JAM1. */
  135.   JAM1,       /* DrawMode, do not change the background. */
  136.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  137.               /* This will leave enough space for the check mark. */
  138.   1,          /* TopEdge, 1 line down. */
  139.   NULL,       /* TextAttr, default font. */
  140.   "Bold",     /* IText, the string. */
  141.   NULL        /* NextItem, no link to other IntuiText structures. */
  142. };
  143.  
  144. /* The MenuItem structure for the second subitem: */
  145. struct MenuItem my_second_subitem=
  146. {
  147.   &my_third_subitem, /* NextItem, linked to the third subitem. */
  148.   50,              /* LeftEdge, 50 pixels out. */
  149.   15,              /* TopEdge, 15 lines down. */
  150.   150,             /* Width, 150 pixels wide. */
  151.   10,              /* Height, 10 lines high. */
  152.   ITEMTEXT|        /* Flags, render this item with text. */
  153.   ITEMENABLED|     /*        this item will be enabled. */
  154.   CHECKIT|         /*        it is an attribute item. */
  155.   HIGHCOMP,        /*        complement the colours when highlihted. */
  156.   0x00000001,      /* MutualExclude, mutualexclude the first subitem. */
  157.   (APTR) &my_second_text, /* ItemFill, pointer to the text. */
  158.   NULL,            /* SelectFill, nothing since we complement the col. */
  159.   0,               /* Command, no command-key sequence. */
  160.   NULL,            /* SubItem, ignored by Intuition. */
  161.   MENUNULL,        /* NextSelect, no items selected. */
  162. };
  163.  
  164.  
  165.  
  166. /*************************************************************************/
  167. /*                       F I R S T   S U B I T E M                       */
  168. /*************************************************************************/
  169.  
  170. /* The text for the first subitem: */
  171. struct IntuiText my_first_text=
  172. {
  173.   2,          /* FrontPen, black. */
  174.   0,          /* BackPen, not used since JAM1. */
  175.   JAM1,       /* DrawMode, do not change the background. */
  176.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  177.               /* This will leave enough space for the check mark. */
  178.   1,          /* TopEdge, 1 line down. */
  179.   NULL,       /* TextAttr, default font. */
  180.   "Plain",    /* IText, the string. */
  181.   NULL        /* NextItem, no link to other IntuiText structures. */
  182. };
  183.  
  184. /* The MenuItem structure for the first subitem: */
  185. struct MenuItem my_first_subitem=
  186. {
  187.   &my_second_subitem, /* NextItem, linked to the second subitem. */
  188.   50,              /* LeftEdge, 50 pixels out. */
  189.   5,               /* TopEdge, 5 lines down. */
  190.   150,             /* Width, 150 pixels wide. */
  191.   10,              /* Height, 10 lines high. */
  192.   ITEMTEXT|        /* Flags, render this item with text. */
  193.   ITEMENABLED|     /*        this item will be enabled. */
  194.   CHECKIT|         /*        it is an attribute item. */
  195.   CHECKED|         /*        this item is initially selected. */
  196.   HIGHCOMP,        /*        complement the colours when highlihted. */
  197.   0xFFFFFFFE,      /* MutualExclude, mutualexclude all items except the */
  198.                    /*                first one. */
  199.   (APTR) &my_first_text, /* ItemFill, pointer to the text. */
  200.   NULL,            /* SelectFill, nothing since we complement the col. */
  201.   0,               /* Command, no command-key sequence. */
  202.   NULL,            /* SubItem, ignored by Intuition. */
  203.   MENUNULL,        /* NextSelect, no items selected. */
  204. };
  205.  
  206.  
  207.  
  208. /*************************************************************************/
  209. /*                       T H E   O N L Y   I T E M                       */
  210. /*************************************************************************/
  211.  
  212. /* The text for the item: */
  213. struct IntuiText my_text=
  214. {
  215.   2,          /* FrontPen, black. */
  216.   0,          /* BackPen, not used since JAM1. */
  217.   JAM1,       /* DrawMode, do not change the background. */
  218.   0,          /* LeftEdge, 0 pixels out. */
  219.               /* No space is needed for a check mark. */
  220.   1,          /* TopEdge, 1 line down. */
  221.   NULL,       /* TextAttr, default font. */
  222.   "Style",    /* IText, the string. */
  223.   NULL        /* NextItem, no link to other IntuiText structures. */
  224. };
  225.  
  226. /* The MenuItem structure for the item: */
  227. struct MenuItem my_item=
  228. {
  229.   NULL,              /* NextItem, no more items after this one. */
  230.   0,                 /* LeftEdge, 0 pixels out. */
  231.   0,                 /* TopEdge, 0 lines down. */
  232.   100,               /* Width, 100 pixels wide. */
  233.   10,                /* Height, 10 lines high. */
  234.   ITEMTEXT|          /* Flags, render this item with text. */
  235.   ITEMENABLED|       /*        this item will be enabled. */
  236.                      /*        it is an action item. (CHECKIT is not set) */
  237.   HIGHCOMP,          /*        complement the colours when highlihted. */
  238.   0,                 /* MutualExclude, no mutualexclude. */
  239.   (APTR) &my_text,   /* ItemFill, pointer to the text. */
  240.   NULL,              /* SelectFill, nothing since we complement the col. */
  241.   0,                 /* Command, no command-key sequence. */
  242.   &my_first_subitem, /* SubItem, pointer to the first subitem. */
  243.   MENUNULL,          /* NextSelect, no items selected. */
  244. };
  245.  
  246.  
  247.  
  248. /*************************************************************************/
  249. /*                              M E N U                                  */
  250. /*************************************************************************/
  251.  
  252. /* The Menu structure for the first (and only) menu: */
  253. struct Menu my_menu=
  254. {
  255.   NULL,        /* NextMenu, no more menu structures. */
  256.   0,           /* LeftEdge, left corner. */
  257.   0,           /* TopEdge, for the moment ignored by Intuition. */
  258.   50,          /* Width, 50 pixels wide. */
  259.   0,           /* Height, for the moment ignored by Intuition. */
  260.   MENUENABLED, /* Flags, this menu will be enabled. */
  261.   "Edit",      /* MenuName, the string. */
  262.   &my_item     /* FirstItem, pointer to the first (and only) item in */
  263.                /* the list. */
  264. };
  265.  
  266.  
  267.  
  268. /* Declare a pointer to a Window structure: */ 
  269. struct Window *my_window;
  270.  
  271. /* Declare and initialize your NewWindow structure: */
  272. struct NewWindow my_new_window=
  273. {
  274.   50,            /* LeftEdge    x position of the window. */
  275.   25,            /* TopEdge     y positio of the window. */
  276.   200,           /* Width       200 pixels wide. */
  277.   100,           /* Height      100 lines high. */
  278.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  279.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  280.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  281.                  /*             user has selected the Close window gad. */
  282.   MENUPICK,
  283.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  284.   WINDOWCLOSE|   /*             Close Gadget. */
  285.   WINDOWDRAG|    /*             Drag gadget. */
  286.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  287.   WINDOWSIZING|  /*             Sizing Gadget. */
  288.   ACTIVATE,      /*             The window should be Active when opened. */
  289.   NULL,          /* FirstGadget No Custom gadgets. */
  290.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  291.   "Style Editor",/* Title       Title of the window. */
  292.   NULL,          /* Screen      Connected to the Workbench Screen. */
  293.   NULL,          /* BitMap      No Custom BitMap. */
  294.   80,            /* MinWidth    We will not allow the window to become */
  295.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  296.   300,           /* MaxWidth    than 300 x 200. */
  297.   200,           /* MaxHeight */
  298.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  299. };
  300.  
  301.  
  302.  
  303. main()
  304. {
  305.   /* Boolean variable used for the while loop: */
  306.   BOOL close_me;
  307.  
  308.   /* Declare a variable in which we will store the IDCMP flag: */
  309.   ULONG class;
  310.   
  311.   /* If we recieve a MENUPICK event, the Code field of the message */
  312.   /* structure will contain the menu number of the first selected item. */
  313.   /* Declare a variable to store the Code value in, and an extra menu */
  314.   /* number variable: */
  315.   USHORT code, menu_number;
  316.   
  317.   /* Declare a MenuItem pointer: */
  318.   struct MenuItem *item;
  319.   
  320.   /* Declare a pointer to an IntuiMessage structure: */
  321.   struct IntuiMessage *my_message;
  322.  
  323.  
  324.  
  325.   /* Before we can use Intuition we need to open the Intuition Library: */
  326.   IntuitionBase = (struct IntuitionBase *)
  327.     OpenLibrary( "intuition.library", 0 );
  328.   
  329.   if( IntuitionBase == NULL )
  330.     exit(); /* Could NOT open the Intuition Library! */
  331.  
  332.  
  333.  
  334.   /* We will now try to open the window: */
  335.   my_window = (struct Window *) OpenWindow( &my_new_window );
  336.   
  337.   /* Have we opened the window succesfully? */
  338.   if(my_window == NULL)
  339.   {
  340.     /* Could NOT open the Window! */
  341.     
  342.     /* Close the Intuition Library since we have opened it: */
  343.     CloseLibrary( IntuitionBase );
  344.  
  345.     exit();  
  346.   }
  347.  
  348.  
  349.  
  350.   /* We have opened the window, and everything seems to be OK. */
  351.  
  352.  
  353.  
  354.   SetMenuStrip( my_window, &my_menu );
  355.   printf("Menustrip connected to window!\n");
  356.  
  357.  
  358.   close_me = FALSE;
  359.  
  360.   /* Stay in the while loop until the user has selected the Close window */
  361.   /* gadget: */
  362.   while( close_me == FALSE )
  363.   {
  364.     /* Wait until we have recieved a message: */
  365.     Wait( 1 << my_window->UserPort->mp_SigBit );
  366.  
  367.     /* As long as we collect messages sucessfully we stay in the loop: */
  368.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  369.     {
  370.       /* After we have collected the message we can read it, and save any */
  371.       /* important values which we maybe want to check later: */
  372.       class = my_message->Class;
  373.       code = my_message->Code;
  374.  
  375.  
  376.       /* After we have read it we reply as fast as possible: */
  377.       /* REMEMBER! Do never try to read a message after you have replied! */
  378.       /* Some other process has maybe changed it. */
  379.       ReplyMsg( my_message );
  380.  
  381.       /* Check which IDCMP flag was sent: */
  382.       if( class == CLOSEWINDOW )
  383.         close_me=TRUE; /* The user selected the Close window gadget! */  
  384.  
  385.       if(class == MENUPICK)
  386.       {
  387.         printf("\nMenu pick!\n");
  388.         menu_number = code;
  389.         
  390.         while( menu_number != MENUNULL )
  391.         {
  392.           /* Get the address of the item: */
  393.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  394.  
  395.  
  396.           /* Print out the menu number plus etc: */
  397.           printf("menu_number= %d\n", menu_number );
  398.           printf("MENUNUM = %d\n", MENUNUM(menu_number) );
  399.           printf("ITEMNUM = %d\n", ITEMNUM(menu_number) );
  400.           printf("SUBNUM  = %d\n", SUBNUM(menu_number) );
  401.  
  402.  
  403.           /* Get the following item's menu number: */
  404.           menu_number = item->NextSelect;
  405.         }
  406.       }
  407.     }
  408.   }
  409.  
  410.  
  411.  
  412.   printf("Menustrip removed from window!\n");
  413.   ClearMenuStrip( my_window );
  414.  
  415.  
  416.  
  417.   /* Close the window: */
  418.   CloseWindow( my_window );
  419.  
  420.  
  421.  
  422.   /* Close the Intuition Library since we have opened it: */
  423.   CloseLibrary( IntuitionBase );
  424.   
  425.   /* THE END */
  426. }
  427.